package com.eye.catcher.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eye.catcher.common.RedisUtils;
import com.eye.catcher.entity.TSysDaSwitch;
import com.eye.catcher.param.CommonResponse;
import com.eye.catcher.param.ResponseData;
import com.eye.catcher.param.ResponseUtil;
import com.eye.catcher.service.generate.ITSysDaSwitchService;
import com.eye.catcher.util.IVerifyCodeGen;
import com.eye.catcher.util.SimpleCharVerifyCodeGenImpl;
import com.eye.catcher.util.VerifyCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huzhiwei
 * @since 2022-03-18
 */
@Slf4j
@RestController
@RequestMapping("/eyeCatcher-switch")
public class TSysDaSwitchController extends BaseController {


    @Autowired
    private ITSysDaSwitchService switchService;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @return
     */
//    @ApiOperation(value = "获取验证码开关")
    @GetMapping("/getVerifCodeInfo")
    @ResponseBody
    public ResponseData getVerificationCodeInfo() {
        try{
            QueryWrapper<TSysDaSwitch> wrapper = Wrappers.query();
            wrapper.eq("code","Verification_code");
            TSysDaSwitch sysDaSwitch = switchService.getOne(wrapper);
            //获取验证码
            commonResponse = CommonResponse.success();
            commonResponse.setData(sysDaSwitch);
        }catch (Exception e){
            e.printStackTrace();
            commonResponse = CommonResponse.error();
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
//    @ApiOperation(value = "获取验证码")
    @GetMapping("/getVerifyCode")
    public ResponseData getVerifyCode(@RequestParam(value = "storageId") String storageId) {
        try{
            //获取验证码
            commonResponse = CommonResponse.success();
            commonResponse.setData(redisUtils.get(storageId));
        }catch (Exception e){
            e.printStackTrace();
            commonResponse = CommonResponse.error();
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
//    @ApiOperation(value = "获取验证码图片")
    @GetMapping("/getVerifyCodeImg")
    public void getVerifyCodeImg(HttpServletRequest request, HttpServletResponse response,
                   @RequestParam(value = "storageId") String storageId) {
        IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
        try {
//设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
            String code = verifyCode.getCode();
            log.info(code);
            redisUtils.set(storageId,code,60*10);
//将VerifyCode绑定session
            request.getSession().setAttribute("VerifyCode", code);
//设置响应头
            response.setHeader("Pragma", "no-cache");
//设置响应头
            response.setHeader("Cache-Control", "no-cache");
//在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
//设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            log.info("", e);
        }
    }
}
